﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using RoketSolutions.SP2010.Infrostructure.UI.Ajax.Abstractions;

namespace RoketSolutions.SP2010.Infrostructure.UI.Ajax.Controllers
{
    public class ActionDescriptor
    {
        public ActionDescriptor(ControllerDescriptor controller, MethodInfo methodInfo)
        {
            Controller = controller;
            MethodInfo = methodInfo;
        }

        public ControllerDescriptor Controller
        {
            get; 
            private set;
        }        

        public MethodInfo MethodInfo
        {
            get; 
            private set;
        }        

        public ParameterInfo[] GetParameters()
        {
            return MethodInfo.GetParameters();
        }

        public virtual Object Execute(ControllerContext context, IDictionary<String, Object> parameters)
        {
            if (MethodInfo != null)
            {
                if (parameters != null && parameters.Values.Count > 0)
                    return MethodInfo.Invoke(context.Controller, parameters.Values.ToArray());
                return MethodInfo.Invoke(context.Controller, new object[0]);
            }
            return null;
        }        
    }
}
